<?php
namespace Platform\Common;

use PhalApi\Exception\BadRequestException;
//use Platform\Model\Admin as AdminModel;
/**
 * Domain层公用方法
 */
class Functions {
    protected $tokenData = array();
    public function __construct() {
        //获取当前管理员权限，只有为超级管理员才能查看
        $this->tokenData = \PhalApi\DI()->platformAdmin->check();
//        $this->tokenData = array(
//            'role_id'=>1,
//            'id'=>1,
//        );
    }

    //自动填充的数据信息 ---新增
    public function selfAddFields($useData){
//        $model=new AdminModel();
//        $useData['row_id']=$model->getUU();
        $useData['add_who']=$this->tokenData['id'];
        $useData['add_who_name']=$this->tokenData['name'];
        $useData['add_time']=time();
        return $useData;
    }

    //自动填充的数据信息 ---编辑
    public function selfEditFields($useData){
        $useData['update_who']=$this->tokenData['id'];
        $useData['update_who_name']=$this->tokenData['name'];
        $useData['update_time']=time();
        return $useData;
    }

    //自动填充的数据信息 ---删除
    public function selfDeleteFields($useData){
        $useData['delete_who']=$this->tokenData['id'];
        $useData['delete_who_name']=$this->tokenData['name'];
        $useData['delete_time']=time();
        $useData['delete_flag']='Y';
        return $useData;
    }

    //全局搜索条件(数据权限筛选)初始化 菜单权限根据返回值前台进行校验，所以不需要后台处理
    public function getCommonWhere($where = array()){
        //超级管理员不需要拼接项目查询条件
        $where['delete_flag'] = 'N';
        if( $this->tokenData['role_name'] == '超级管理员' ){
            return $where;
        }
        // todo 可继续扩展公共的查询条件 project_code要为数组形式
        if(!isset($where['project_code'])){
            $project_code = array();
            if( count($this->tokenData['project_info']) > 0 ){
                foreach ( $this->tokenData['project_info'] as $k => $v ){
                    $project_code[] = $v['project_code'];
                }
                $where['project_code'] = $project_code;
            }else{
                throw new BadRequestException('非法登录', 8);
            }
        }
        return $where;
    }

    //菜单获取树方法
    public function _getTree($data)
    {
        //第一步初始化数据
        $items = array();
        foreach($data as $value){
            $items[$value['authorityId']] = $value;
        }
        //第二部 遍历数据 生成树状结构
        $tree = array();
        foreach($items as $key => $item){
            if(isset($items[$item['parentId']])){
                $items[$item['parentId']]['children'][] = &$items[$key];
            }else{
                $tree[] = &$items[$key];
            }
        }
        return $tree;
    }

    /**
     * 二维数组根据某个字段去重
     * @param array $array  二维数组
     * @para array  去重后的数组
     */
    public function uniquArr($array,$key){
        $result = array();
        foreach($array as $k=>$val){
            $code = false;
            foreach($result as $_val){
                if($_val[$key] == $val[$key]){
                    $code = true;
                    break;
                }
            }
            if(!$code){
                $result[]=$val;
            }
        }
        return $result;
    }

    //解密js前台加密的代码
    public function decode($str){
        $staticchars = "PXhw7UT1B0a9kQDKZsjIASmOezxYG4CHo5Jyfg2b8FLpEvRr3WtVnlqMidu6cN";
        $decodechars = "";
        for($i=1;$i<strlen($str);){
            $num0 = strpos($staticchars, $str[$i]);
            if($num0 !== false){
                $num1 = ($num0+59)%62;
                $code = $staticchars[$num1];
            }else{
                $code = $str[$i];
            }
            $decodechars .= $code;
            $i+=3;
        }
        return $decodechars;
    }

    //加密密码
    public function encryptPassword($password, $salt) {
        return md5(md5(sha1($password . $salt)));
    }

    // 生成文件编码
    public function getEncoding($name){
        $fileCode = $name.date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        return $fileCode;
    }
}
